package com.hiv.controller.admin;

import com.hiv.common.BaseController;
import com.hiv.common.PageInfo;
import com.hiv.common.WebResponse;
import com.hiv.common.param.page.PageQuerySamplingRecordParam;
import com.hiv.common.response.InspectionStatisticsInfo;
import com.hiv.common.response.StorageStatisticsInfo;
import com.hiv.mapper.domain.Admin;
import com.hiv.utils.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hiv.mapper.domain.SamplingRecord;
import com.hiv.service.SamplingRecordService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author zhanghong
 * @version AdminSamplingRecordController.java, v 0.1 2022-09-20 AdminSamplingRecordController Exp $$
 */
@RestController
@RequestMapping("/admin/samplingRecord")
@Api(value = "采样数据模块", tags = {"【管理端】采样数据模块"})
public class AdminSamplingRecordController extends BaseController {
    @Autowired
    private SamplingRecordService samplingRecordService;

    @ApiOperation(value = "根据id查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<SamplingRecord> getById(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(samplingRecordService.getById(id));
    }

    @ApiOperation(value = "分页查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<SamplingRecord>> pageQuery(HttpServletRequest request, @RequestBody PageQuerySamplingRecordParam param) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.pageQuery(admin, param));
    }

    @ApiOperation(value = "入库", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/updateStatus")
    public WebResponse<Boolean> updateStatus(HttpServletRequest request, @RequestBody SamplingRecord param) {
        Admin admin = validAdminLoginAndGet(request);
        param.setStorageTime(new Date());
        return WebResponse.build(samplingRecordService.updateStatus(admin, param));
    }

    @ApiOperation(value = "根据条形码查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/getByBarCode/{barCode}")
    public WebResponse<Boolean> getByBarCode(HttpServletRequest request, @PathVariable String barCode) {
        validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.getByBarCode(barCode));
    }

    @ApiOperation(value = "删除", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/delete")
    public WebResponse<Boolean> delete(HttpServletRequest request, @RequestParam String ids) {
        validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.deleteByIds(ids));
    }

    @ApiOperation(value = "提交检测", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/inspection")
    public WebResponse<Boolean> inspection(HttpServletRequest request, @RequestBody SamplingRecord param) {
        Admin admin = validAdminLoginAndGet(request);
        param.setInspectionTime(new Date());
        return WebResponse.build(samplingRecordService.inspection(admin, param));
    }

    @ApiOperation(value = "入库统计", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/storage/statistics")
    public WebResponse<StorageStatisticsInfo> storageStatistics(HttpServletRequest request) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.storageStatistics(admin));
    }

    @ApiOperation(value = "检测统计", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/inspection/statistics")
    public WebResponse<InspectionStatisticsInfo> inspectionStatistics(HttpServletRequest request) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.inspectionStatistics(admin));
    }

    @ApiOperation(value = "上传病载检验结果excel", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/upload/excel")
    public WebResponse<Boolean> uploadExcelAndResolving(HttpServletRequest request, @RequestParam("file") MultipartFile file,
                                                        @RequestParam(value = "id", required = false) Long id,
                                                        @RequestParam(value = "inspectionId", required = false) Long inspectionId) throws Exception {
        return new WebResponse<>(samplingRecordService.uploadExcelAndResolving(file, id, inspectionId));
    }

    @ApiOperation(value = "上传耐药检验结果txt", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/upload/txt")
    public WebResponse<Boolean> uploadTxtAndResolving(HttpServletRequest request, @RequestParam("file") MultipartFile file, @RequestParam("id") Long id) throws Exception {
        return new WebResponse<>(samplingRecordService.uploadTxtAndResolving(file, id));
    }

    @ApiOperation(value = "报告完善列表", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/authorizedStrengthPageQuery")
    public WebResponse<PageInfo<SamplingRecord>> authorizedStrengthPageQuery(HttpServletRequest request, @RequestBody PageQuerySamplingRecordParam param) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.authorizedStrengthPageQuery(admin, param));
    }

    @ApiOperation(value = "报告完善提交", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/completeSampleInfo")
    public WebResponse<Boolean> completeSampleInfo(HttpServletRequest request, @RequestBody SamplingRecord param) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.completeSampleInfo(admin, param));
    }

    @ApiOperation(value = "报告审核列表", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/auditPageQuery")
    public WebResponse<PageInfo<SamplingRecord>> auditPageQuery(HttpServletRequest request, @RequestBody PageQuerySamplingRecordParam param) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.auditPageQuery(admin, param));
    }

    @ApiOperation(value = "报告审核列表-通过", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/auditPass/{id}")
    public WebResponse<Boolean> auditPass(HttpServletRequest request, @PathVariable("id") long id) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.auditPass(admin, id));
    }

    @ApiOperation(value = "报告审核列表-驳回", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/auditReject/{id}")
    public WebResponse<Boolean> auditReject(HttpServletRequest request, @PathVariable("id") long id) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.auditReject(admin, id));
    }

    @ApiOperation(value = "报告批准列表", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/ratifyPageQuery")
    public WebResponse<PageInfo<SamplingRecord>> ratifyPageQuery(HttpServletRequest request, @RequestBody PageQuerySamplingRecordParam param) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.ratifyPageQuery(admin, param));
    }

    @ApiOperation(value = "报告批准列表-通过", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/ratifyPass/{id}")
    public WebResponse<SamplingRecord> ratifyPass(HttpServletRequest request, @PathVariable("id") long id) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.ratifyPass(admin, id));
    }

    @ApiOperation(value = "报告批准列表-驳回", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/ratifyReject/{id}")
    public WebResponse<Boolean> ratifyReject(HttpServletRequest request, @PathVariable("id") long id) {
        Admin admin = validAdminLoginAndGet(request);
        return WebResponse.build(samplingRecordService.ratifyReject(admin, id));
    }

    @ApiOperation(value = "上传签名图片", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/upload/signImage")
    public WebResponse<String> uploadSignImage(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
        return new WebResponse<>(FileUploadUtils.upload("/sign", file));
    }

    @ApiOperation(value = "上传pdf图片", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/upload/pdf")
    public WebResponse<String> uploadPdf(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
        return new WebResponse<>(samplingRecordService.uploadReportPdf(request, file));
    }

    @ApiOperation(value = "更新报pdf地址", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/update/report/url")
    public WebResponse<Boolean> updateReportUrl(HttpServletRequest request, @RequestBody SamplingRecord record) throws Exception {
        return new WebResponse<>(samplingRecordService.updateReportUrl(record));
    }
}
